<?php
/**
 * @Author: Xu Liu email:2867557054@qq.com
 * @Date:   2016-12-15 13:31:33
 * @Last Modified by:   Xu Liu
 * @Last Modified time: 2018-01-03 21:17:46
 */

if (!defined('IN_ADMINCP') || !defined('IN_DISCUZ')) {
    exit('Access Denied');
}
class MtopicContrl
{
    public $cachekey = array(
        '1_type',
        '2_type',
        '3_type',
        '4_type',
        '5_type',
        '6_type',
        '7_type',
        '8_type',
        '9_type',
        '10_type',
        '11_type',
        '12_type',
        '13_type',
        '14_type',
        '15_type',
        '16_type',
        '17_type',
        '38_type',
        '44_type',
        '55_type',
        '66_type',
        '77_type',
        '88_type',
        '101_type',
        '102_type',
        '103_type',
        '120_type',
        '201_type',
        '1_type',
        '1_type',
        '1_type',
        '1_type',
        'jamesonreadnewzhuanti',
        'jamesonreadpcadv_3_4',
        'jamesonreadpcadv_2_0',
        'jamesonreadpcadv_11_12',
    );
    public function index()
    {
        global $_G;
        $topics = C::t('#jameson_read#jamesonread_topics')->fetch_by_type(1);
        foreach ($topics as $key => $value) {
            $topics[$key]['image'] = unserialize($value['image']);
        }
        $books    = C::t('#jameson_read#jamesonread_books')->all();
        $toutiaos = C::t('#jameson_read#jamesonread_topics')->fetch_by_type(5);
        include template('jameson_read:mtopic_index');
    }
    /*pc adv*/
    public function adv()
    {
        global $_G;
        // 首页横幅广告
        $indexadv = C::t('#jameson_read#jamesonread_topics')->fetch_by_type(2);
        // 分类页右侧
        $cateadv = C::t('#jameson_read#jamesonread_topics')->fetch_by_type(3);
        // 目录页右侧
        $contentadv = C::t('#jameson_read#jamesonread_topics')->fetch_by_type(4);
        // pc阅读页上下
        $pcyueduadv = C::t('#jameson_read#jamesonread_topics')->fetch_by_type(7);
        include template('jameson_read:mtopic_adv');
    }
    /*手机版*/
    public function mobile()
    {
        global $_G;
        // 手机幻灯
        $shoujiadv = C::t('#jameson_read#jamesonread_topics')->fetch_by_type(6);
        if (is_file("./xifeng/api.php")) {
            $appgonggao  = C::t('#jameson_read#jamesonread_topics')->fetch_by_type(44);
            $appguanggao = C::t('#jameson_read#jamesonread_topics')->fetch_by_type(55);
            $apphuandeng = C::t('#jameson_read#jamesonread_topics')->fetch_by_type(66);
            $appzhuanti  = C::t('#jameson_read#jamesonread_topics')->fetch_by_type(77);
            $appurl      = C::t('common_setting')->fetch('jamesonreadappurl');
            $appurl      = $appurl ? $appurl : '';
            $appabout    = C::t('#jameson_read#jamesonread_topics')->fetch_by_type(88);
        }
        // 手机侧栏导航
        $shoujicelannav = C::t('#jameson_read#jamesonread_topics')->fetch_by_type(10);
        // 手机底部导航 type=120
        $dibunav = C::t('#jameson_read#jamesonread_topics')->fetch_by_type(120);
        // 手机阅读器页广告
        $shoujiyueduadv = C::t('#jameson_read#jamesonread_topics')->fetch_by_type(8);
        // 发现页面广告 11
        $faxianadv = C::t('#jameson_read#jamesonread_topics')->fetch_by_type(11);
        // 书城广告12
        $shuchengadv = C::t('#jameson_read#jamesonread_topics')->fetch_by_type(12);
        // 手机版书架好书推荐
        $haoshutuijian = C::t('#jameson_read#jamesonread_topics')->fetch_by_type(13);
        $noheaderbar = (int) C::t('common_setting')->fetch('xf512read_noheaderbar');
        $shoujianniu =  C::t('common_setting')->fetch('xf512read_mbstyle');
        include template('jameson_read:mtopic_mobile');
    }
    // 专题post
    public function post()
    {
        global $_G;
        if (submitcheck('editsubmit')) {
            foreach ($_GET['name'] as $key => $value) {
                if (trim($value)) {
                    $data           = array();
                    $data['name']   = daddslashes(dhtmlspecialchars($value));
                    $data['descro'] = daddslashes($_GET['descro'][$key]);
                    $data['enable'] = 1;
                    $data['image']  = serialize($_GET['book_ids'][$key]);
                    C::t('#jameson_read#jamesonread_topics')->update($key, $data);
                }
            }
            foreach ($_GET['image'] as $key => $value) {
                $data        = array();
                $data['image'] = daddslashes(htmlspecialchars_decode($value));
                $data['url'] = daddslashes($_GET['url'][$key]);
                C::t('#jameson_read#jamesonread_topics')->update($key, $data);
            }
            C::t('#jameson_read#jamesonread_topics')->clear_cache($this->cachekey);
            cpmsg(lang('plugin/jameson_read', 'gengxinchenggong'), 'action=plugins&operation=config&do=' . $_GET['do'] . '&identifier=jameson_read&pmod=jameson_read&contrl=mtopic&act=index', 'succeed');
        } else {
            cpmsg(lang('plugin/jameson_read', 'laiyuanfeifa'), 'action=plugins&operation=config&do=' . $_GET['do'] . '&identifier=jameson_read&pmod=jameson_read&contrl=mtopic&act=index', 'error');
        }
    }
    // pc adv post
    public function advpost()
    {
        global $_G;
        if (submitcheck('editsubmit')) {
            foreach ($_GET['image'] as $key => $value) {
                $data        = array();
                $data['adv'] = daddslashes(htmlspecialchars_decode($value));
                C::t('#jameson_read#jamesonread_topics')->update($key, $data);
            }
            C::t('#jameson_read#jamesonread_topics')->clear_cache($this->cachekey);
            cpmsg(lang('plugin/jameson_read', 'gengxinchenggong'), 'action=plugins&operation=config&do=' . $_GET['do'] . '&identifier=jameson_read&pmod=jameson_read&contrl=mtopic&act=adv', 'succeed');
        } else {
            cpmsg(lang('plugin/jameson_read', 'laiyuanfeifa'), 'action=plugins&operation=config&do=' . $_GET['do'] . '&identifier=jameson_read&pmod=jameson_read&contrl=mtopic&act=adv', 'error');
        }
    }
    // 手机版 post
    public function mbadvpost()
    {
        global $_G;
        if (submitcheck('editsubmit')) {
            foreach ($_GET['hdimage'] as $key => $value) {
                // 手机幻灯
                $data          = array();
                $data['image'] = daddslashes(($value));
                $data['url']   = daddslashes(($_GET['hdurl'][$key]));
                $data['adv']   = daddslashes(($_GET['hdtitle'][$key]));
                C::t('#jameson_read#jamesonread_topics')->update($key, $data);
            }
            foreach ($_GET['celanimage'] as $key => $value) {
                // 手机侧栏菜单
                $data          = array();
                $data['image'] = daddslashes(($value));
                $data['url']   = daddslashes(($_GET['celanurl'][$key]));
                C::t('#jameson_read#jamesonread_topics')->update($key, $data);
            }
            foreach ($_GET['dibunavadv'] as $key => $value) {
                // 底部导航
                $data          = array();
                $data['adv']   = daddslashes(($value));
                $data['url']   = daddslashes(($_GET['dibunavurl'][$key]));
                $data['image'] = daddslashes(($_GET['dibunavimage'][$key]));
                C::t('#jameson_read#jamesonread_topics')->update($key, $data);
            }
            foreach ($_GET['gonggaotitle'] as $key => $value) {
                // app公告
                $data          = array();
                $data['image'] = daddslashes(($value));
                $data['url']   = daddslashes(($_GET['gonggaourl'][$key]));
                C::t('#jameson_read#jamesonread_topics')->update($key, $data);
            }
            // if (isset($_GET['appguanggaoimage'])) {
            //     // app广告
            //     foreach ($_GET['appguanggaoimage'] as $key => $value) {
            //         $data          = array();
            //         $data['image'] = daddslashes(($value));
            //         $data['url']   = daddslashes(($_GET['appguanggaourl'][$key]));
            //         $data['adv']   = daddslashes(($_GET['appguanggaobiaoti'][$key]));
            //         C::t('#jameson_read#jamesonread_topics')->update($key, $data);
            //     }
            // }
            if (isset($_GET['apphuandengimage'])) {
                // app幻灯
                foreach ($_GET['apphuandengimage'] as $key => $value) {
                    $data          = array();
                    $data['image'] = daddslashes(($value));
                    $data['url']   = daddslashes(($_GET['apphuandengurl'][$key]));
                    $data['adv']   = daddslashes(($_GET['apphuandengbiaoti'][$key]));
                    C::t('#jameson_read#jamesonread_topics')->update($key, $data);
                }
            }
            // if (isset($_GET['appzhuantiimage'])) {
            //     // app专题
            //     foreach ($_GET['appzhuantiimage'] as $key => $value) {
            //         $data           = array();
            //         $data['image']  = daddslashes(($value));
            //         $data['adv']    = daddslashes(($_GET['appzhuantiadv'][$key]));
            //         $data['name']   = daddslashes(($_GET['appzhuantiname'][$key]));
            //         $data['descro'] = daddslashes(($_GET['appzhuantidescro'][$key]));
            //         C::t('#jameson_read#jamesonread_topics')->update($key, $data);
            //     }
            // }
            if (isset($_GET['appaboutcontent'])) {
                // app关于我们
                foreach ($_GET['appaboutcontent'] as $key => $value) {
                    $data = array();

                    $data['adv'] = daddslashes($value);
                    C::t('#jameson_read#jamesonread_topics')->update($key, $data);
                }
            }
            foreach ($_GET['hstjadv'] as $key => $value) {
                $data        = array();
                $data['adv'] = daddslashes(htmlspecialchars_decode($value));
                C::t('#jameson_read#jamesonread_topics')->update($key, $data);
            }
            // 手机广告
            foreach ($_GET['adv'] as $key => $value) {
                $data        = array();
                $data['adv'] = daddslashes(htmlspecialchars_decode($value));
                C::t('#jameson_read#jamesonread_topics')->update($key, $data);
            }
            if (DB::result_first('SELECT count(*) FROM %t WHERE ' . DB::field('skey', 'jamesonreadappurl'), array('common_setting'))) {
                C::t('common_setting')->update('jamesonreadappurl', daddslashes($_GET['appdownloadurl']));
            } else {
                DB::insert('common_setting', array('skey' => 'jamesonreadappurl', 'svalue' => daddslashes($_GET['appdownloadurl'])));
            }
            if (DB::result_first('SELECT count(*) FROM %t WHERE ' . DB::field('skey', 'xf512read_noheaderbar'), array('common_setting'))) {
                C::t('common_setting')->update('xf512read_noheaderbar', intval($_GET['noheaderbar']));
            } else {
                DB::insert('common_setting', array('skey' => 'xf512read_noheaderbar', 'svalue' => intval($_GET['noheaderbar'])));
            }
            C::t('common_setting')->update('xf512read_mbstyle', daddslashes($_GET['shoujianniu']));
            C::t('#jameson_read#jamesonread_topics')->clear_cache($this->cachekey);
            cpmsg(lang('plugin/jameson_read', 'gengxinchenggong'), 'action=plugins&operation=config&do=' . $_GET['do'] . '&identifier=jameson_read&pmod=jameson_read&contrl=mtopic&act=mobile', 'succeed');
        } else {
            cpmsg(lang('plugin/jameson_read', 'laiyuanfeifa'), 'action=plugins&operation=config&do=' . $_GET['do'] . '&identifier=jameson_read&pmod=jameson_read&contrl=mtopic&act=mobile', 'error');
        }
    }
    public function toutiaopost()
    {
        global $_G;
        if (submitcheck('editsubmit')) {
            foreach ($_GET['image'] as $key => $value) {
                if (trim($value)) {
                    $data          = array();
                    $data['image'] = daddslashes($value);
                    $data['url']   = daddslashes($_GET['url'][$key]);
                    C::t('#jameson_read#jamesonread_topics')->update($key, $data);
                }
            }
            C::t('#jameson_read#jamesonread_topics')->clear_cache($this->cachekey);
            cpmsg(lang('plugin/jameson_read', 'gengxinchenggong'), 'action=plugins&operation=config&do=' . $_GET['do'] . '&identifier=jameson_read&pmod=jameson_read&contrl=mtopic&act=index', 'succeed');
        } else {
            cpmsg(lang('plugin/jameson_read', 'laiyuanfeifa'), 'action=plugins&operation=config&do=' . $_GET['do'] . '&identifier=jameson_read&pmod=jameson_read&contrl=mtopic&act=index', 'error');
        }
    }
}